package Week10;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.io.IOException;
import java.net.URL;



public class polyLineInOut {
	private ArrayList<PolyLine> polyLineListin, polyLineListOut;


	public polyLineInOut(ArrayList<PolyLine> polyLineListin) {
		this.polyLineListin = polyLineListin;
		this.polyLineListOut = polyLineListOut;
	}

	public ArrayList<PolyLine> getPolyLineListin() {
		return polyLineListin;
	}

	public void setPolyLineListin(ArrayList<PolyLine> polyLineListin) {
		this.polyLineListin = polyLineListin;

	}
	
	//Write array list of PolyLines to a user-specified binary file.
	public void writeFile(ArrayList<PolyLine> polyLineListin, File selectedFile){
		try{
			FileOutputStream fos = new FileOutputStream(selectedFile);
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject(polyLineListin);
			oos.flush();
			oos.close();
		}
		catch(IOException e){
			e.printStackTrace();
		}


	}
	
	//Read array list of PolyLines from a user-specified binary file.
	public ArrayList<PolyLine> readFile(File selectedFile){
		try{
			FileInputStream fis = new FileInputStream(selectedFile);
			ObjectInputStream ois = new ObjectInputStream(fis);
			polyLineListOut = (ArrayList<PolyLine>) ois.readObject();
			ois.close();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return polyLineListOut;

	}
	
	//Read array list of PolyLines from the web
	public ArrayList<PolyLine> readFileWeb(){
		try{
			URL polyLineUrl = new URL("https://sites.google.com/site/myintrotogis/home/my-file-cabinet/Week10-Polylines.bif");
			ObjectInputStream ois = new ObjectInputStream((InputStream) polyLineUrl.getContent());
			polyLineListOut = (ArrayList<PolyLine>) ois.readObject();
			ois.close();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return polyLineListOut;

	}
}